home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / C++ A'Link Files / 1990 / Oct 90 / CPlus.Dev$ 10⁄12⁄90 / 0224-How do I use virtual-Oct90 < prev    next >
Encoding:
Text File  |  1991-03-06  |  1.7 KB  |  46 lines  |  [TEXT/GEOL]

  1. Item forwarded  by  THOUGHT.SHOP to MADA.LIB
  2.  
  3. Item    3772254                         10-Oct-90        14:03PDT
  4.  
  5. From:   DEREK                           White, Derek
  6.  
  7. To:     CPLUS.DEV$                      C++ Interest List--Developers
  8.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  9.  
  10. Sub:    How do I use virtual op== ?
  11.  
  12.     Is there a way to make operator== work polymorphically and "correctly"?  If
  13. I have references to two objects, then I would like the most specific op== if
  14. the operands are the same type.  Here's an example  (assuming class TShape is
  15. the base class for TRect):
  16.  
  17. foo(TShape& a, TShape& b) {
  18.     if (a == b)
  19.        ; // beep, whatever...
  20. }
  21.  
  22. main {
  23.     TShape aShape;
  24.     TRect aRect;
  25.  
  26.     foo(aShape, aShape);    // should call TShape::op==(TShape&)
  27.     foo(aShape, aRect);     // should call TShape::op==(TShape&) ?
  28.     foo(aRect, aShape);     // should call TShape::op==(TShape&) ?
  29.     foo(aRect, aRect);      // should call TRect::op==(TRect&)<-different type!
  30. }
  31.  
  32.     The point I ran across here is that if TShape::op==(TShape&) is virtual,
  33. then TRect has to declare TRect::op==(TShape&).  But what's the point of
  34. comparing TRects and TShapes?  The base class's op== will work just fine unless
  35. the parameter to TRect::op==(TShape&) really is a TRect, but from inside
  36. TRect::op== how can you tell?
  37.     The other point is that I tend to think of equality as a symmetric
  38. operation.  (a == b) should equal (b == a).
  39.     This isn't very clear, the example isn't realistic, and the syntax is
  40. probably messed up, but I hope somebody gets the idea.  Is there a way to make
  41. this work, or should I forget virtual op==?  (Maybe virtual symmetric
  42. operators}
  43.  
  44. Derek White
  45.  
  46.